var app = getApp();
Page({
  data: {
    username: null,
    password: null,
    canIUse:false
  },

  onLoad: function (options) {
    //用户授权登录
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']){
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo)
            }
          })
        }else {
          this.setData({
            canIUse:true
          })
        }
      }
    }),
    wx.login({
      success:function(res){
        var code = res.code;//登录凭证
        if (code) {
          var data = {
            jscode: code
          }
        }
        var url = 'getToken';
        wx.showLoading({
          title: "验证中...",
          mask: true
        });
        app.func.req(data, url, function (res) {
          console.info('登录返回结果', res);
          console.info('code', res.data.code);
          if (res.data.code == 0) {
            console.info(res.data.msg);
            wx.showToast({
              title: res.data.msg
            });
            //登录成功存储staff
            app.globalData.staffId = res.data.staff.staffId;
            app.globalData.staffName = res.data.staff.staffName;
            app.globalData.mobile = res.data.staff.mobile;
            app.globalData.password = res.data.staff.password;
            app.globalData.token = res.data.token;
            console.info(app.globalData.staffId);
            wx.navigateTo({ url: "../chooseItem/chooseItem"})
          } else {
            wx.hideLoading();
            wx.showToast({
              title: '自动登录失败'
            });
          }
        });
      }
    })

  },
  bindGetUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo;
  },

  usernameInput: function (event) {
    this.setData({ username: event.detail.value })
  },
  passwordInput: function (event) {
    this.setData({ password: event.detail.value })
  },
  //登录
  loginBtnClick: function (e) {

    //获取用户名称和头像      
    //调用应用实例的方法获取全局数据
    var that = this;
    var phone = this.data.username;
    wx.login({
      success: function (res) {
        var code = res.code;//登录凭证
        var data = {
          loginName: phone,
          password: "123456",// this.data.password
           jscode: code
        }
        var url = 'stafflogin';
        wx.showLoading({
          title: "验证中...",
          mask: true
        });
        wx.setEnableDebug({
          enableDebug: true
        })
        app.func.req(data, url, function (res) {
          console.info('登录返回结果', res);
          console.info('code', res.data.code);
          if (res.data.code == 0) {
            console.info(res.data.msg);
            wx.showToast({
              title: res.data.msg
            });
            //登录成功存储staff
            app.globalData.staffId = res.data.staff.staffId;
            app.globalData.staffName = res.data.staff.staffName;
            app.globalData.mobile = res.data.staff.mobile;
            app.globalData.password = res.data.staff.password;
            app.globalData.token = res.data.token;
            console.info(app.globalData.staffId);
            wx.navigateTo({ url: "../chooseItem/chooseItem" })
          } else {
            wx.hideLoading();
            wx.showToast({
              title: '登录失败'
            });
          }
        });
      }, 
    })
  
   
  },
})

